# -*- coding: utf-8 -*-

import numpy as np
import cv2
from matplotlib import pyplot as plt

def cv_show(img, name, writename):  #
    cv2.imshow(name, img)
    cv2.imwrite(writename, img)
    cv2.waitKey()
    cv2.destroyAllWindows()
# 读取原始图像
img = cv2.imread('mario.jpg')
#对图像进行二值化
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 读取模板图像
template = cv2.imread('mario_coin.jpg',0)
# 获取模板高h和长w的范围
h,w = template.shape[:2]
# 设定匹配方法
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCORR_NORMED)  #
threshold = 0.8
#获取匹配程序大于0.8的坐标
loc = np.where(res >=threshold)
# 开始匹配
for pt in zip(*loc[::-1]):#*表示可选参数
    bottom_right = (pt[0] + w,pt[1]+ h )
    cv2.rectangle(img,pt,bottom_right,(0,0,255),2)
cv2.imshow('img', img)
cv2.waitKey()
cv2.imwrite('mario_end.png', img)
cv2.destroyAllWindows()

